<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 添加按钮 -->

      <el-row type="flex" justify="end">
        <el-col>
          <!-- 定义前面得插槽 -->
          <el-input v-model="input" placeholder="请输入内容" style="width:20%;margin-right:10px" />
          <el-button @click="clear">清除</el-button>
          <el-button type="primary" @click="serch">搜索</el-button>
        </el-col>
        <el-button justify="end" float="right" type="success" size="small" @click="addPermission">
          <i class="el-icon-edit" />新增权限组</el-button>
      </el-row>
      <el-alert
        style="margin-top:10px;margin-bottom:10px"
        type="info"
        show-icon
        :closable="false"
      >共{{ counts }}条记录</el-alert>
      <el-table
        ref="multipleTable"
        :header-cell-style="{ background: '#fafafa',borderBottom:'2px solid #e8e8e8'}"
        :data="tableData"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          style="marge_right:200px"
        />
        <el-table-column
          prop="title"
          label="用户名"
        />
        <el-table-column
          prop="create_date"
          label="日期"
          sortable
        >
          <template v-slot="scope">
            {{ scope.row.create_date | formatDate }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          show-overflow-tooltip
        >
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" circle @click="editpermission(scope.row.id)" />
            <el-button type="danger" icon="el-icon-delete" circle @click="deletepermission(scope.row.id)" />
          </template>

        </el-table-column>
      </el-table>
      <!-- 添加,编辑弹框 -->
      <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="用户名" prop="title">
            <el-input v-model="roleForm.title" />
          </el-form-item>
          <el-form-item label="权限标题" prop="permissions">
            <el-tree
              ref="permTree"
              :data="list"
              :props="defaultProps"
              :default-expand-all="true"
              show-checkbox
              :default-checked-keys="permissions"
              node-key="id"
            />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <!-- 分页组件 -->
        <el-pagination
          :current-page="page.page"
          :page-size="page.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
  </div>

</template>

<script>
import { permissionGroupModification, permissionGroup, permissionGroupAdd, permissionGroupDeletion, permissionGroupDetails, simplePermissionGroup } from '@/api/permission'
import { menuList } from '@/api/menu'
import { tranListToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      id: '',
      input: '',
      checkAll: false,
        checkedCities: [],
        isIndeterminate: true,
       roleForm: {
        title: '',
        permissions: []
       },
      rules: {
        title: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        permissions: [{ required: false, trigger: 'blur' }]
      },
      showDialog: false,
      title: '创建权限组',
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10
      },
      total: 0, // 记录总数
      permissions: [], // 定义一个数组来接收 已经选中的节点
      defaultProps: {
        children: 'childs',
          code: 'code',
          label: 'title' // 表示 从这个属性显示内容
      },
      list: [],
      count: 1,
      tableData: [],
      multipleSelection: [],
      counts: ''

    }
  },
  created() {
    this.permissionGroup(this.page)
    this.simplePermissionGroup()
  },
  methods: {
    async  simplePermissionGroup() { // 获取权限组的简单列表
       await simplePermissionGroup()
    },
    async menuList() { // 菜单详情
      const res = await menuList()
      console.log(res, 'ews88888')
      this.list = tranListToTreeData(res, null)
    },
    async permissionGroup(data) { // 获取权限组的列表
      const res = await permissionGroup(data)
      this.tableData = res.list
      this.counts = res.counts
     this.total = this.counts
    },
    addPermission() { // 添加权限组按钮
      this.showDialog = true
      this.menuList()

      console.log(this.roleForm.permissions, '  this.roleForm.permissions')
    },
    async editpermission(id) { // 编辑菜单按钮
      // console.log(data, 'dadadad')
      this.id = id
      this.title = '编辑权限组'
      this.showDialog = true
      this.menuList()
      // 详细信息
      const res = await permissionGroupDetails(id)
      console.log(res.permissions, 'res.data.permissions')
      this.roleForm = res
      this.permissions = res.permissions
    },
    async deletepermission(id) { // 菜单删除
     try {
 await this.$confirm('确定要删除吗')
       await permissionGroupDeletion(id)
       this.permissionGroup({ page: this.page.page, pagesize: this.page.pagesize, title: this.input })

       this.$message.success('删除成功')
     } catch (error) {
      this.$message.info('删除失败')
      }
      // // 分页跳转
      if (this.tableData.length === 1) {
        this.page.page = this.page.page - 1
      }
      this.permissionGroup({ page: this.page.page, pagesize: this.page.pagesize, title: this.input })
    },
    clear() { // 清除
      this.input = ''
      this.permissionGroup()
    },
    serch() { // 搜索
    const text = this.input
      const reg = new RegExp(text) // 声明正则变量
      this.tableData = this.tableData.filter(item => reg.test(item.title))
      this.permissionGroup({ page: this.page.page, pagesize: this.page.pagesize, title: this.input })
    },
    async btnOK() {
      this.$refs.roleForm.validate(async(flag) => {
        if (flag) {
          if (this.title === '编辑权限组') {
        const data = {
          permissions: this.$refs.permTree.getCheckedKeys(),
          title: this.roleForm.title,
          id: this.id }
         await permissionGroupModification(data)
      } else {
await permissionGroupAdd({ permissions: this.$refs.permTree.getCheckedKeys(), title: this.roleForm.title })
      }

     this.permissionGroup()
     this.$message.success('操作成功')
      this.showDialog = false
        } else {
        this.$message.info('需要填写完整哦')
      }
       })
    },
    btnCancel() { // 取消按钮
     this.roleForm = {
        title: '',
        permissions: []
      }
       this.permissions = []
      this.$refs.roleForm.resetFields()

      this.showDialog = false
    },
      // 下面是复选框
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row)
          })
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
// 下面是分页
// ---------------------------------------------------------------
    handleCurrentChange(val) {
      this.page.page = val
console.log(this.page)
    this.permissionGroup(this.page)
      }
  }

}
</script>
<style scoped>
 .menu_title{
  margin-top: 20px;
  color: #909399;
  background-color: #fafafa;
  height:60px;
  line-height: 50px;
}

</style>
